package com.lcicl.reggie.controller;
/*
  Date: 2023/11/01
  Author: Luochao
  Description:
*/

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lcicl.reggie.common.BaseContext;
import com.lcicl.reggie.common.CustomException;
import com.lcicl.reggie.common.R;
import com.lcicl.reggie.entity.OrderDetail;
import com.lcicl.reggie.entity.Orders;
import com.lcicl.reggie.entity.dto.OrdersDto;
import com.lcicl.reggie.service.OrderDetailService;
import com.lcicl.reggie.service.OrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    OrderDetailService orderDetailService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders order) {

        orderService.submit(order);

        return R.success("下单成功");
    }


    @GetMapping("/userPage")
    public R<Page<OrdersDto>> userPage(int page, int pageSize) {
        Page<Orders> orderPage = new Page<>(page, pageSize);
        Page<OrdersDto> dtoPage = new Page<>();

        Long userId = BaseContext.getCurrentId();
//        Long userId = 13388388239L;
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId, userId).orderByDesc(Orders::getOrderTime);
        orderService.page(orderPage, queryWrapper);

        //订单List
        List<Orders> orderRecords = orderPage.getRecords();
        if (orderRecords.size() == 0) {
//            throw new CustomException("订单不存在！");
            return R.error("订单不存在！");
        }

        //封装OrdersDto的List
        List<OrdersDto> dtoRecords = orderRecords.stream().map(order -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(order, ordersDto);
            String orderId = order.getNumber();
            LambdaQueryWrapper<OrderDetail> orderDetailWrapper = new LambdaQueryWrapper<>();
            orderDetailWrapper.eq(OrderDetail::getOrderId, orderId);
            List<OrderDetail> orderDetailList = orderDetailService.list(orderDetailWrapper);
            ordersDto.setOrderDetails(orderDetailList);
            return ordersDto;
        }).collect(Collectors.toList());

        //封装回Page
        BeanUtils.copyProperties(orderPage, dtoPage, "records");
        dtoPage.setRecords(dtoRecords);

        return R.success(dtoPage);
    }


    /**
     * 后台分页查询
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page<OrdersDto>> page(@RequestParam(value = "page") Integer page,
                                   @RequestParam(value = "pageSize") Integer pageSize,
                                   @RequestParam(value = "number", required = false) String number,
                                   @RequestParam(value = "beginTime", required = false) String beginTime,
                                   @RequestParam(value = "endTime", required = false) String endTime
    ) {

        Page<Orders> orderPage = new Page<>(page, pageSize);
        Page<OrdersDto> dtoPage = new Page<>();


        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Orders::getOrderTime);// 按订单时间降序排列
        queryWrapper.like(StringUtils.isNotEmpty(number), Orders::getNumber, number);

        boolean between = StringUtils.isNotEmpty(beginTime) && StringUtils.isNotEmpty(endTime);
        queryWrapper.between(between, Orders::getOrderTime, beginTime, endTime);

        orderService.page(orderPage, queryWrapper);

        //订单List
        List<Orders> orderRecords = orderPage.getRecords();
//        if (orderRecords.size() == 0) {
////            throw new CustomException("订单不存在！");
//            return R.error("订单不存在！");
//        }

        //封装OrdersDto的List
        List<OrdersDto> dtoRecords = orderRecords.stream().map(order -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(order, ordersDto);

            String orderId = order.getNumber();
            LambdaQueryWrapper<OrderDetail> orderDetailWrapper = new LambdaQueryWrapper<>();
            orderDetailWrapper.eq(OrderDetail::getOrderId, orderId);
            List<OrderDetail> orderDetailList = orderDetailService.list(orderDetailWrapper);

            ordersDto.setOrderDetails(orderDetailList);
            return ordersDto;
        }).collect(Collectors.toList());

        //封装回Page
        BeanUtils.copyProperties(orderPage, dtoPage, "records");
        dtoPage.setRecords(dtoRecords);

        return R.success(dtoPage);
    }

    /**
     * 修改订单状态
     * @param orders
     * @return
     */
    @PutMapping()
    public R<String> put(@RequestBody Orders orders) {
        System.out.println(orders);

        boolean b = orderService.updateById(orders);
        System.out.println(b);


        return b ? R.success("派送成功") : R.error("派送失败，请重试");
    }

}
